package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.Goods;
import com.bean.ShopingCar;
import com.serve.GoodsService;
import com.serve.ShopService;
import com.serveImple.GoodsServiceImpl;
import com.serveImple.ShopServiceImpl;

/**
 * Servlet implementation class ShopServlet
 */
@WebServlet("/ShopServlet")
public class ShopServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShopServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String name= request.getParameter("goods");
		
		GoodsService goodsservice=new GoodsServiceImpl();
		
		int number =0;
		if(name!=null){
			 number=Integer.parseInt(name);				
		}
		Goods goods=goodsservice.showGoods(number);
		
		String str=request.getParameter("number");
		System.out.println(str);
		if(str!=null){
			int num=Integer.parseInt(str);
			ShopingCar shop=new ShopingCar();
			shop.setG_price(goods.getG_price());
			shop.setS_name(goods.getG_name());
			shop.setS_address(goods.getG_address());
			shop.setSc_number(num);
		ShopService shopservice=new ShopServiceImpl();
		int i=shopservice.add(shop);
		if(i==1){
			System.out.println("加入成功");
			request.setAttribute("goods", goods);
			request.getRequestDispatcher("ShopCarServlet").forward(request, response);
		}else{
			System.out.println("加入失败");
			request.setAttribute("jieguo", "加入失败、重新加入");
			request.setAttribute("goods", goods);
			request.getRequestDispatcher("easybuy/shop.jsp").forward(request, response);
		}
		
		}else{
			System.out.println("没有数量");
		request.setAttribute("goods", goods);
		request.getRequestDispatcher("easybuy/shop.jsp").forward(request, response);}
							
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
